package cn.pconline.autoclub.service;

import cn.pconline.autoclub.entity.ClubMember;
import cn.pconline.autoclub.entity.UserLimit;
import cn.pconline.autoclub.util.DateUtils;
import cn.pconline.autoclub.util.Pager;
import cn.pconline.autoclub.util.WebUtils;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.gelivable.dao.GeliDao;
import org.gelivable.dao.SqlBuilder;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author wanganning
 */
public class UserLimitService {

    @Autowired
    private GeliDao geliDao;
     @Autowired
    private PagerService pagerService;
     @Autowired
     private MemberService memberService;
     @Autowired
     private UserService userService;

    /**
     * 查找某天的会员限制信息,不会为空
     *
     * @param uid 用户id
     * @param ymd 日期,yyyyMMdd格式
     * @return
     */
    public UserLimit getMemberLimit(long uid, int ymd) {
        String sql = "SELECT * FROM ac_user_limit WHERE user_id = ? AND ymd = ?";
        UserLimit limit = geliDao.findFirst(UserLimit.class, sql, uid, ymd);
        if (limit == null) {
        	limit = new UserLimit();
        	limit.setUid(uid);
            limit.setYmd(DateUtils.formatData2YMD());
            geliDao.create(limit);
        }
        return limit;
    }

    /**
     * 查找当天的会员限制信息,不会为空
     *
     * @param uid 用户id
     * @return
     */
    public UserLimit getMemberLimit(long uid) {
        return getMemberLimit(uid, DateUtils.formatData2YMD());
    }

    /**
     * 更新会员限制信息
     * @param uid 用户id
     * @param petrol 加分值
     * @return
     */
    public int update(long uid, int petrol) {
        String sql = "SELECT * FROM ac_user_limit WHERE user_id = ? AND ymd = ?";
        int time = DateUtils.formatData2YMD();
        UserLimit limit = geliDao.findFirst(UserLimit.class, sql, uid, time);
        if (limit == null) { // 新增
        	limit = new UserLimit();
            limit.setUid(uid);
            limit.setYmd(time);
            limit.setPetrol(petrol);
            limit.setCreateAt(DateUtils.getNow());
            long limitId = geliDao.create(limit);
            return limitId > 0 ? 1 : 0;
        }
        
        // 更新
        limit.setPetrol(limit.getPetrol() + petrol);
        return geliDao.update(limit);
    }
    /**
     * 查询某段时间内会员油值大于petrol的信息
     * @param beginDate 开始时间 
     * @param endDate 结束时间  (不允许跨月 精确到天)
     * @param petrol 油值
     * @return 
     */
    public Pager<UserLimit> getUserLimitByDate(String beginDate,String endDate,int petrol,int pageNo,int pageSize){
         SqlBuilder sql = new SqlBuilder();
        sql.appendSql("SELECT * FROM ac_user_limit where 1=1 ");
        if(StringUtils.isNotBlank(beginDate)){
            int begin = Integer.valueOf(DateUtils.formatData2YMD(WebUtils.parseDate(beginDate)));
            sql.appendSql("and ymd >= ").appendValue(begin);
        }
        if(StringUtils.isNotBlank(endDate)){
            int end = Integer.valueOf(DateUtils.formatData2YMD(WebUtils.parseDate(endDate)));
            sql.appendSql(" and ymd <= ").appendValue(end);
        }
        sql.appendSql(" and petrol >=").appendValue(petrol).appendSql(" and DATE_ADD(createAt,INTERVAL 5 HOUR_MINUTE)>= updateAt order by ymd desc");
                
        Pager<UserLimit> pager = pagerService.pager(UserLimit.class, sql,pageNo,pageSize);
        List<UserLimit> list = pager.getResultList();
        for(UserLimit u:list){
            ClubMember member = memberService.find(u.getUid());
            if(member!=null){
                  u.setUser(userService.findUser(member.getUid()));
            }
        }
        return pager;
    }
}
